<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
 * this class handles all the news and incidents
 */

class News extends CI_Controller {
//    $news = array('created' => '', 'Author' => '', 'title' => '', 'slug' => '', 'article' => '', 'aid' => '', 'public' => false, 'ntype' => [array('News, incident') ] 
//        'comments' => [array('cdate' => '', 'writer' => '', 'comment' => '')], 'archive' => true );
    public $datah ;
    
	public function __construct()
	{
		parent::__construct();
		$this->load->model('news_model');
                $this->load->model('menu_model');
                session_start();
                $role = 'public';   // predefine the var 
                if (isset($_SESSION)) {
                    if ( isset($_SESSION['logged_in'])) {
                        $role = $_SESSION['role']; } else { $role = 'public'; } } 
            
            $this->datah['role'] = $role;   
            $this->datah['menu'] = $this->menu_model->getmenu( $role ,'news' );
          
             // LOAD LIBRARIES
            $this->load->library(array('encrypt', 'form_validation' ));

            // LOAD HELPERS
            $this->load->helper(array('url', 'form', 'xml'));
            
            $this->datah['showmenu'] = TRUE;
                
	}

	public function index()
	{
        $page = "News";    
	$data['title'] = 'News archive';

        if ( ! file_exists('application/views/news/newsview.php'))
            {
		// Whoops, we don't have a page for that!
		show_404();
            }
        $this->datah['title'] = 'News';
        $this->datah['baseurl'] = base_url();
	$this->datah['title'] = ucfirst($page); // Capitalize the first letter
        
        $this->vaidnewsadd();
        	
        if( $this->input->post('Add') )  // is the Add button set
            {
                if( $this->form_validation->run() == TRUE ) {  // run the validation
                   // Validation is OK
                   redirect(base_url().'index.php/news');
                } else {
                    // Validation Failed
                    $this->datah['news'] = $this->news_model->getallnewsbyarea($_SESSION['curarea'], 'News');
                    $this->load->view('templates/header', $this->datah );
                    $this->load->view('news', $this->datah);
                    $this->load->view('templates/footer');
                    
                }
            }  else {
                    /* Display no post */
                    $this->datah['news'] = $this->news_model->getallnewsbyarea($_SESSION['curarea'], 'News');
                    
                    $this->load->view('templates/header', $this->datah );
                    $this->load->view('templates/sidebar', $this->datah );
                    $this->load->view('news/newsview', $this->datah);
                    $this->load->view('templates/footer');
            }   
   
	}

	public function view($slug)
	{
            /*
             * Returns a news item
             */
            
            $this->datah['baseurl'] = base_url();
            $data['news_item'] = $this->news_model->get_news($slug);
            
            if (empty($data['news_item']))
            {
		show_404();
            }

            $data['title'] =  $data['news_item'][0]['title'];
            //print_r($data['news_item']);
            $this->load->view('templates/header', $this->datah);
            $this->load->view('news/view', $data);
            $this->load->view('templates/footer');
	}
        
        public function create()
{
        /*
         * Creates a new news item
         */    
	$this->load->helper('form');
	$this->load->library('form_validation');
	$this->datah['baseurl'] = base_url();

	$this->form_validation->set_rules('title', 'Title', 'required');
	$this->form_validation->set_rules('article', 'News', 'required');
	if( $this->input->post('newnews') )  
          {
            if ($this->form_validation->run() === FALSE)
            {   /* Validation Failed */
                $data['title'] = $this->input->post('title');
                $data['article'] = $this->input->post('article');
		$this->load->view('templates/header', $this->datah);
                $this->load->view('news/create', $data);
		$this->load->view('templates/footer');
	    } else {
                /* Validation success */
               $data['title'] = $this->input->post('title');
               $data['article'] = $this->input->post('article'); 
               $this->news_model->set_news( $data);
	       $this->load->view('news/success');
	    }
          } else {
                /* No Post */
                $data['title'] = '';
                $data['article'] = '';
		$this->load->view('templates/header', $this->datah);
                $this->load->view('news/create', $data);
		$this->load->view('templates/footer');
          }   
}
        
function vaidnewsadd() {
    $this->form_validation->set_rules('title', 'Title', 'required');
    $this->form_validation->set_rules('article', 'News', 'required');    
}
        
        
}
?>
